package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.ValveTransferBean;
import com.koron.css2.clientCenter.bean.ValveTransferHistoryBean;
import com.koron.css2.clientCenter.mapper.ValveCloseDetailMapper;
import com.koron.css2.clientCenter.mapper.ValveTransferHistoryMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Date;

/**
 * @author cd
 * 转办
 */
public class ValveTransfer implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ValveTransferBean transferBean = JsonUtils.objectToPojo(req.getData(), ValveTransferBean.class);
            ValveCloseDetailMapper detailMapper = factory.getMapper(ValveCloseDetailMapper.class);
            if ("1".equals(transferBean.getType())) {
                detailMapper.updateCloseOperator(transferBean.getId(), transferBean.getName(), transferBean.getAccount(),
                        userInfo.getUserInfo().getAcount(), userInfo.getUserInfo().getName());
            } else if ("2".equals(transferBean.getType())) {
                detailMapper.updateOpenOperator(transferBean.getId(), transferBean.getName(), transferBean.getAccount(),
                        userInfo.getUserInfo().getAcount(), userInfo.getUserInfo().getName());
            } else {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "转办失败，type不正确", null);
            }
            ValveTransferHistoryMapper historyMapper = factory.getMapper(ValveTransferHistoryMapper.class);
            ValveTransferHistoryBean bean = new ValveTransferHistoryBean(transferBean);
            bean.setId(Tools.getObjectId());
            bean.setSponsor(userInfo.getUserInfo().getName());
            bean.setSponsorAccount(userInfo.getUserInfo().getAcount());
            bean.setCreateTime(new Date());
            historyMapper.insert(bean);
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "转办成功", null);
        } catch (Exception e) {
            logger.error("转办失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "转办失败", null);
        }
    }
}
